package Client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Date;

public class UDPOfficerClient {
	
	private String badgeID=null;
	// private static int officerCount = 1786;
	String serverName =null;

// return BadgeID
	public String getBadgeID() {
		return badgeID;
	}

	public UDPOfficerClient(String stationName, String badgeID) throws IOException {

		this.badgeID = badgeID; // stationName + officerCount;
		//createOfficerLogs(operation, recordID, fName, lName, desc, status, badgeId);
		//officerCount++;
	}// end of UDPOfficerClient Constructor

	private void createOfficerLogs(String operation, String recordID, String fName, String lName, String desc, String status, String badgeId) {
		File logfile; 
		BufferedWriter bw = null;
    	Date today = new Date();
    	
    	try{
    		
    		//.replaceAll("[0-9]","")
    	// for each station server we have to maintain one file containing all records
    		logfile = new File("Officer_" + badgeID + ".txt"); // file name : Officer_badgeID.txt
    		logfile.createNewFile();
  
    		FileWriter fw = new FileWriter(logfile.getName(), true);
    		bw = new BufferedWriter(fw);
    		
    		String details = "RecordID:: " + recordID + " First Name:: " + fName + " Last Name:: " + lName +
					   " Description:: " + desc + " Status:: " + status + " Date::" + today;
    		
    		bw.write(details); // write the details onto the file.
    		bw.newLine();
    		    		
    	}// end of the try block
    	
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}// end of exception
    	
    	// lose the file after writing the logs
    	try
    	{
    		bw.close();
    	}
    	catch (Exception ee)
    	{
    		ee.printStackTrace();
    	}
    	
	//	return true;
	
} // end of method createOffierLogs

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		
		DatagramSocket aSocket = null;
		int choice =0 ; // default choice set to 0
		int port =0; // set port to 0 initially
		
		try {
			
			InputStreamReader isr = new InputStreamReader(System.in);
			BufferedReader br2 = new BufferedReader(isr);
			
			do {
				
				System.out.println("Welcome to Distributed Police Information System \n\n");
					
				System.out.print("Your menu options are as follows :: \n" +
								"1. Create a NEW CRIMINAL RECORD \n" +
								"2. Create a NEW MISSING RECORD  \n" +
								"3. GET Record COUNTS 		     \n" +
								"4. EDIT an existing RECORD		 \n" +
								"5. TRANSFER record 			 \n" +
								"6. EXIT						 \n" +
								"Please enter your choice now... \n");
				
				choice = Integer.parseInt(br2.readLine());				
				
				try {
					aSocket = new DatagramSocket();
					StringBuilder sb=new StringBuilder();
					sb.append(Integer.toString(choice)+",");
					
					byte[] msg =new byte[5000];
					
				switch(choice)
				{
				
				
				//		"1. Create a NEW CRIMINAL RECORD \n"
				case 1: 	
					UDPOfficerClient client1 = new UDPOfficerClient("SPVM", "SPVM1111");
					sb.append("Salim ,Sopariwalla ,Case1 - Commit a crime , status, SPVM1111"+ client1.getBadgeID());
					port = 6789; // SPVM = port[0] = 6789
					break;

				//		"2. Create a NEW MISSING RECORD  \n" +
				case 2: 
					UDPOfficerClient client2 = new UDPOfficerClient("SPVM","SPVM2222");
					sb.append("Salim ,Sopariwalla ,Case2 - Missing Record, Missing status, SPVM2222"+ client2.getBadgeID());
					port = 6789; // SPVM = port[0] = 6789
					break;
					
				
				//		"3. GET Record COUNTS 		     \n" +
				case 3: 
					UDPOfficerClient officer3 = new UDPOfficerClient("SPVM","SPVM3333");
					sb.append(officer3.getBadgeID());
					port=6789;
					break;
					
				//		"4. EDIT an existing RECORD		 \n" +
				case 4 : 
					UDPOfficerClient officer4 = new UDPOfficerClient("SPVM","SPVM4444");
					sb.append("Sopariwalla , CR20000, Edited the Record," + officer4.getBadgeID());
					port=6789;
					break;
					
					
				//		"5. TRANSFER record 			 \n" + 
				case 5: 
					
					break;
					
					
				// 		"6. EXIT						 \n" +		
				case 6:
					System.out.println("Thank you for using DPIS today...");
					System.exit(0);
					break;
					
				
				default:
					System.out.println("Please enter a correct choice.\n");
				
				} // end of switch
				
				
				// UDP starts here 
				
				String requestMessage=sb.toString();
				sb=new StringBuilder();
				msg=requestMessage.getBytes(); // preparing the request message
				InetAddress aHost = InetAddress.getByName("localhost");
				DatagramPacket request = new DatagramPacket(msg, msg.length, aHost,	port);
				System.out.println(msg+"\n\n");
				aSocket.send(request);
				
				byte[] buffer = new byte[5000];
				DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
				aSocket.receive(reply);
				
				String result = new String(reply.getData());
				
				System.out.println(result);
				
				
				requestMessage=null;
				msg=new byte[5000];
				
				} // end of inner try loop
			
				catch (SocketException se) 
				{
					System.out.println("Socket Exception : " + se.getMessage());
				} 
				
				catch (IOException ioe)
				{
					System.out.println("IO Exception : "  + ioe.getMessage());
				} 
				
				finally {
					if (aSocket != null)
						aSocket.close();
				} // end finally block
				
			}// end of do loop
			
			while(choice!=6);
		
		} //end of try block
		
		catch (Exception e) {
			e.printStackTrace();
		} // end of catch block
		
	} // end of main	
	
} // end of class UDPOfficerClient
